# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

default_platform(:ios)

platform :ios do
  desc "Submit a new Beta build to Apple TestFlight"
  lane :beta do

    api_key = app_store_connect_api_key(
      key_id: "AU3T8TCRLN",
      issuer_id: "95830311-9e99-47b0-95ac-d628a3e4215e",
      key_filepath: "fastlane/AuthKey_AU3T8TCRLN.p8",
      duration: 1200, # optional
      in_house: false, # optional but may be required if using match/sigh
    )
    
    increment_build_number({
      build_number: latest_testflight_build_number(version: get_version_number(target: "Rainbow")) + 1
    })

    update_info_plist(
      plist_path: "Rainbow/Info.plist",
      display_name: "Rainbow"
    )

    update_app_identifier(
      app_identifier: "me.rainbow",
      xcodeproj: "Rainbow.xcodeproj",
      plist_path: "Rainbow/Info.plist",
    )

    match(
      readonly: true,
      type: "appstore",
      app_identifier: ["me.rainbow", "me.rainbow.PriceWidget", "me.rainbow.SelectTokenIntent", "me.rainbow.ImageNotification", "me.rainbow.OpenInRainbow", "me.rainbow.ShareWithRainbow"],
      git_url: "git@github.com:rainbow-me/rainbow-code-signing.git"
    )
    
    gym(
      workspace: "Rainbow.xcworkspace",
      scheme: "Rainbow",
      include_symbols: true,
      configuration: "Release",
      export_method: "app-store",
      export_options: {
        signingStyle: "manual",
        provisioningProfiles: {
          "me.rainbow" => "match AppStore me.rainbow",
          "me.rainbow.PriceWidget" => "match AppStore me.rainbow.PriceWidget",
          "me.rainbow.SelectTokenIntent" => "match AppStore me.rainbow.SelectTokenIntent",
          "me.rainbow.ImageNotification" => "match AppStore me.rainbow.ImageNotification",
          "me.rainbow.OpenInRainbow" => "match AppStore me.rainbow.OpenInRainbow",
          "me.rainbow.ShareWithRainbow" => "match AppStore me.rainbow.ShareWithRainbow",
        },
      }
    )

    pilot(
      app_identifier: "me.rainbow",
      skip_waiting_for_build_processing: true,
      changelog:  "#{ENV['BITRISE_GIT_BRANCH']} - #{ENV['BITRISE_GIT_MESSAGE']}",
    )

  end
  desc "Create new build for iOS devices in release mode"
  lane :build_device do

    api_key = app_store_connect_api_key(
      key_id: "63N65C2G2S",
      issuer_id: "95830311-9e99-47b0-95ac-d628a3e4215e",
      key_filepath: "fastlane/AuthKey_63N65C2G2S.p8",
      duration: 1200, # optional
      in_house: false, # optional but may be required if using match/sigh
    )

    update_info_plist(
      plist_path: "Rainbow/Info.plist",
      display_name: "Rainbow"
    )

    update_app_identifier(
      app_identifier: "me.rainbow",
      xcodeproj: "Rainbow.xcodeproj",
      plist_path: "Rainbow/Info.plist",
    )

    match(
      force: true,
      type: "adhoc",
      app_identifier: ["me.rainbow", "me.rainbow.PriceWidget", "me.rainbow.SelectTokenIntent", "me.rainbow.ImageNotification", "me.rainbow.OpenInRainbow", "me.rainbow.ShareWithRainbow"],
      git_url: "git@github.com:rainbow-me/rainbow-code-signing.git",
    )

    gym(
      workspace: "Rainbow.xcworkspace",
      scheme: "Rainbow",
      include_symbols: false,
      export_method: "ad-hoc",
      output_directory: "build",
      output_name: "Rainbow.ipa",
      archive_path: "build/Rainbow.xcarchive",
      export_options: {
        signingStyle: "manual",
        provisioningProfiles: {
          "me.rainbow" => "match AdHoc me.rainbow",
          "me.rainbow.PriceWidget" => "match AdHoc me.rainbow.PriceWidget",
          "me.rainbow.SelectTokenIntent" => "match AdHoc me.rainbow.SelectTokenIntent",
          "me.rainbow.ImageNotification" => "match AdHoc me.rainbow.ImageNotification",
          "me.rainbow.OpenInRainbow" => "match AdHoc me.rainbow.OpenInRainbow",
          "me.rainbow.ShareWithRainbow" => "match AdHoc me.rainbow.ShareWithRainbow",
        },
      }
    )

  end
end
